package equipments

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/equipments"
	equipmentsReq "github.com/flipped-aurora/gin-vue-admin/server/model/equipments/request"
)

type EquipmentService struct {
}

// CreateEquipment 创建equipment表记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentService *EquipmentService) CreateEquipment(equipment *equipments.Equipment) (err error) {
	err = global.GVA_DB.Create(equipment).Error
	return err
}

// DeleteEquipment 删除equipment表记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentService *EquipmentService) DeleteEquipment(equipment equipments.Equipment) (err error) {
	err = global.GVA_DB.Delete(&equipment).Error
	return err
}

// DeleteEquipmentByIds 批量删除equipment表记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentService *EquipmentService) DeleteEquipmentByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]equipments.Equipment{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateEquipment 更新equipment表记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentService *EquipmentService) UpdateEquipment(equipment equipments.Equipment) (err error) {
	err = global.GVA_DB.Save(&equipment).Error
	return err
}

// GetEquipment 根据id获取equipment表记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentService *EquipmentService) GetEquipment(id uint) (equipment equipments.Equipment, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&equipment).Error
	return
}

// GetEquipmentInfoList 分页获取equipment表记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentService *EquipmentService) GetEquipmentInfoList(info equipmentsReq.EquipmentSearch) (list []equipments.Equipment, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&equipments.Equipment{})
	var equipments []equipments.Equipment
	// 如果有条件搜索 下方会自动创建搜索语句

	where := db.Where("1=1")

	if info.EquipmentKind != "" {
		where = where.Where("equipment_kind= ?", info.EquipmentKind)
	}
	if info.EquipmentName != "" {
		where = where.Where("equipment_name=?", info.EquipmentName)
	}
	if info.EquipmentNum != "" {
		where = where.Where("equipment_num=?", info.EquipmentNum)
	}
	if info.ProductionStatus != "" {
		where = where.Where("production_status=?", info.ProductionStatus)
	}

	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&equipments).Error
	return equipments, total, err
}

type Number struct {
	EquipmentKind string `gorm:"equipment_kind" json:"equipment_kind"`
	Count         int64  `json:"count" gorm:"count"`
	Slave         int64  `json:"slave" gorm:"slave"`
}

func (equipmentService *EquipmentService) GetEquipmentNumber() (info []*Number, err error) {

	err = global.GVA_DB.Table("equipment").Select("equipment_kind,count(equipment_kind) as count").Group("equipment_kind").Scan(&info).Error

	for _, v := range info {
		var count int64
		err = global.GVA_DB.Table("equipment").Where("equipment_kind=? and production_status= '维修中'", v.EquipmentKind).Count(&count).Error
		v.Slave = count
	}

	return info, err
}

type DataInfo struct {
	EquipmentName string `gorm:"equipment_name" json:"equipment_name"`
	EquipmentType string `gorm:"equipment_type" json:"equipment_type"`
	Location      string `json:"location" gorm:"location"`
	Total         int    `json:"total" gorm:"total"`
}

type DataType struct {
	Use  []DataInfo
	Bad  []DataInfo
	Rest []DataInfo
}

func (equipmentService *EquipmentService) GetEquipmentDevice() (info DataType, err error) {
	fmt.Println("iininininn")

	err = global.GVA_DB.Table("equipment").Select("equipment_name,equipment_type,location").Where("production_status =?", "使用中").Find(&info.Use).Error

	err = global.GVA_DB.Table("equipment").Select("equipment_name,equipment_type,location").Where("production_status =?", "维修中").Find(&info.Bad).Error

	err = global.GVA_DB.Table("equipment").Select("equipment_name,equipment_type,location").Where("production_status =?", "空闲中").Find(&info.Rest).Error

	return info, err
}

func (equipmentService *EquipmentService) GetEquipmentBad(pageinfo request.GetDataSearch) (Bad []DataInfo, err error) {
	limit := pageinfo.BadSize
	offset := pageinfo.BadSize * (pageinfo.BadPage - 1)
	var count int64
	global.GVA_DB.Table("equipment").Select("equipment_name,equipment_type,location").Where("production_status =?", "使用中").Count(&count)
	err = global.GVA_DB.Table("equipment").Select("equipment_name,equipment_type,location").Where("production_status =?", "维修中").Limit(int(limit)).Offset(int(offset)).Find(&Bad).Error
	fmt.Println("iininininn")
	fmt.Println(Bad)
	Bad[0].Total = int(count)
	return Bad, err
}

func (equipmentService *EquipmentService) GetEquipmentRest(pageinfo request.GetDataSearch) (Rest []DataInfo, err error) {
	limit := pageinfo.RestSize
	offset := pageinfo.RestSize * (pageinfo.RestPage - 1)
	var count int64
	global.GVA_DB.Table("equipment").Select("equipment_name,equipment_type,location").Where("production_status =?", "空闲中").Count(&count)
	fmt.Println("iininininn")
	err = global.GVA_DB.Table("equipment").Select("equipment_name,equipment_type,location").Where("production_status =?", "空闲中").Limit(int(limit)).Offset(int(offset)).Find(&Rest).Error
	fmt.Println("iininininn")
	fmt.Println(Rest)

	Rest[0].Total = int(count)
	return Rest, err
}

func (equipmentService *EquipmentService) GetEquipmentUse(pageinfo request.GetDataSearch) (Use []DataInfo, err error) {
	limit := pageinfo.UseSize
	offset := pageinfo.UseSize * (pageinfo.UsePage - 1)
	var count int64
	global.GVA_DB.Table("equipment").Select("equipment_name,equipment_type,location").Where("production_status =?", "使用中").Count(&count)
	err = global.GVA_DB.Table("equipment").Select("equipment_name,equipment_type,location").Where("production_status =?", "使用中").Limit(int(limit)).Offset(int(offset)).Find(&Use).Error
	fmt.Println("iininininn")
	fmt.Println(Use)
	Use[0].Total = int(count)
	return Use, err
}
